
use walkdir::{WalkDir, DirEntry, Error};
use std::{collections::LinkedList};

/**
 * 是否是图片
 */
fn is_image(r: &Result<DirEntry, Error>) -> bool {
    match r {
        Ok(dir_entry) => {
            let f = dir_entry.file_name().to_str().unwrap();
            f.ends_with(".jpg") || f.ends_with(".jpeg") || f.ends_with(".png")
        },
        Err(_) => false
    }
    
}

/**
 * 读取图片文件路径
 * @param fileDirPath 文件夹路径
 */
pub fn read_image_path(file_dir_path: &str) -> LinkedList<String> {

    let walkdir = WalkDir::new(file_dir_path);
    let mut list: LinkedList<String> = LinkedList::new();
    
    for entry in  walkdir.into_iter().filter(|r| is_image(r)){
        match entry {
            Ok(dir_entry) => {
                let path = dir_entry.path().display().to_string();
                list.push_back(path);
            },
            Err(_) => {}
        }
    
    }
    return list;
}

#[test]
pub fn test_read_image(){
    let list = read_image_path("C:/Users/mdjro/Desktop/faces");
    for path in list {
        println!("{}", path);
    }
}